package com.mouse.hxsj.view;

import com.mouse.damo.micro.Com;
import com.mouse.damo.micro.Window;
import com.mouse.hxsj.constants.ComPool;
import com.mouse.hxsj.util.ViewGet;
import com.mouse.hxsj.util.ViewSet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

@Slf4j
@Component
public class ProcessIFrm extends JInternalFrame {
    private JTable table;
    private JTextField textField_num;
    private JButton button_delete;
    private JButton button_login;
    private JPanel panel_save;

    private JTextField textField;
    private final static Com com = new Com();
    private final static Window window = new Window(com);

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            try {
                ProcessIFrm frame = new ProcessIFrm();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * Create the frame.
     */
    public ProcessIFrm() {
        setClosable(true);
        setIconifiable(true);
        setTitle("游戏进程");
        setBounds(20, 20, 700, 400);

        JScrollPane scrollPane = new JScrollPane();

        panel_save = new JPanel();
        panel_save.setBorder(new TitledBorder(null, "编辑", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(28)
                                .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                                        .addComponent(panel_save, GroupLayout.PREFERRED_SIZE, 611, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 610, GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(45, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(21)
                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                                .addComponent(panel_save, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE)
                                .addGap(18))
        );

        JLabel label_1 = new JLabel("句柄");

        textField_num = new JTextField();
        textField_num.setEditable(false);
        textField_num.setColumns(10);

        JLabel label_2 = new JLabel("进程");

        JButton button = new JButton("保存");
        button.addActionListener(e -> save());

        button_delete = new JButton("激活");
        button_delete.addActionListener(e -> setWindowState(1));

        button_login = new JButton("最小化");
        button_login.addActionListener(e -> setWindowState(3));

        textField = new JTextField();
        textField.setColumns(10);
        JButton button_4 = new JButton("结束");
        button_4.addActionListener(e -> setWindowState(13));

        JButton button_1 = new JButton("搜索");
        button_1.addActionListener(e -> search());

        GroupLayout gl_panel_1 = new GroupLayout(panel_save);
        gl_panel_1.setHorizontalGroup(
                gl_panel_1.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel_1.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel_1.createSequentialGroup()
                                                .addComponent(label_1)
                                                .addGap(16)
                                                .addComponent(textField_num, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.RELATED, 108, Short.MAX_VALUE)
                                                .addComponent(label_2, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.RELATED))
                                        .addGroup(gl_panel_1.createSequentialGroup()
                                                .addComponent(button, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18)
                                                .addComponent(button_4, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
                                                .addGap(26)
                                                .addComponent(button_login, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)
                                                .addGap(28)))
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel_1.createSequentialGroup()
                                                .addComponent(button_delete, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
                                                .addGap(28)
                                                .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE))
                                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, 287, GroupLayout.PREFERRED_SIZE))
                                .addContainerGap())
        );
        gl_panel_1.setVerticalGroup(
                gl_panel_1.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel_1.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_1)
                                        .addComponent(textField_num, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_2)
                                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(18)
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(button)
                                        .addComponent(button_4)
                                        .addComponent(button_login)
                                        .addComponent(button_delete)
                                        .addComponent(button_1))
                                .addContainerGap(53, Short.MAX_VALUE))
        );
        panel_save.setLayout(gl_panel_1);

        table = new JTable();
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent met) {
                tableSelected(met);
            }
        });
        table.setModel(new DefaultTableModel(
                new Object[][]{
                },
                new String[]{
                        "句柄", "账号"
                }
        ));
        scrollPane.setViewportView(table);
        getContentPane().setLayout(groupLayout);
    }

    private void search() {
        log.info("检测绑定句柄");
        ComPool.getComList().forEach(com -> ComPool.destoryHwnd(com));
        initTable();
    }

    private void setWindowState(int state) {
        Integer hwnd = ViewGet.textField(textField_num, Integer.class);
        if (hwnd == null) return;
        window.setWindowState(hwnd, state);
    }

    private void save() {
        Integer hwnd = ViewGet.textField(textField_num, Integer.class);
        String account = ViewGet.textField(textField, String.class);
        if (hwnd == null) return;
        List<Com> comList = ComPool.getComList();
        List<String> accountList = ComPool.getAccountList();
        for (int i = 0; i < comList.size(); i++) {
            Com com = comList.get(i);
            if (com.getHwnd() == hwnd) {
                accountList.set(i, account);
            }
        }
        initTable();
    }


    public void initFrm() {
        initTable();
    }

    private void tableSelected(MouseEvent met) {
        int row = table.getSelectedRow();
        Integer hwnd = (Integer) table.getValueAt(row, 0);
        String account = (String) table.getValueAt(row, 1);
        ViewSet.textField(textField_num, hwnd);
        ViewSet.textField(textField, account);
    }

    public void initTable() {
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setRowCount(0);
        List<Com> comList = ComPool.getComList();
        List<String> accountList = ComPool.getAccountList();
        for (int i = 0; i < comList.size(); i++) {
            Com com = comList.get(i);
            Vector vector = new Vector();
            vector.add(com.getHwnd());
            vector.add(accountList.get(i));
            tableModel.addRow(vector);
        }
    }
}
